var location = require("../../../mybehaviors/location/location");
var app = getApp()
Page({
  behaviors: [location],

  /**
   * 页面的初始数据
   */
  data: {
    typeList: [{
      value: 1,
      name: "评价",
      sort: "",
      active: false
    },{
      value: 2,
      name: "销售",
      sort: "",
      active: false
    },{
      value: 3,
      name: "价格",
      sort: "",
      active: false
    }],
    permutation: true,
    search_title: "",
    goodsList: [],
    page: 1,
    total: 0,
    finished: false,
    address: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this._getLocation((res) => {
      this.setData({
        address: res
      })
      this.getGoodsList();
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.getGoodsList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  inpText(e) { 
    this.setData({
      goodsList: [],
      page: 1,
      total: 0,
      finished: false ,
      search_title: e.detail.value
    })
    this.getGoodsList();
  },
  clickPermutation() {
    this.setData({
      permutation: !this.data.permutation
    })
  },
  chooseTab(ev) {
    let ob = ev.currentTarget.dataset.item;
    let typeList = this.data.typeList;

    for(let item of typeList) {
      item.active = false;
      if(item.value == ob.value) {
        item.active = ob.sort == "desc" ? false : true;
        item.sort = ob.sort == "asc" ? "desc" : ob.sort == "desc" ? "" : "asc";
      } else {
        item.sort = "" 
      }
    }
    this.setData({
      typeList
    })
    this.initData();
    this.getGoodsList();
  },
  initData() {
    this.setData({
      goodsList: [],
      page: 1,
      total: 0,
      finished: false 
    })
  },
  getGoodsList() {
    if (this.data.finished) return;
    app._postNetWork({
      url: app.getNetAddresss("plugin.supplier-local-life.frontend.index.get-goods-list"),
      data: {
        adcode: this.data.address.ad_info.adcode,
        category_id: "",
        page: this.data.page,
        search: {
          title: this.data.search_title
        },
        order_field: this.data.typeList.filter(item => { return item.active }).length ? this.data.typeList.filter(item => item.active).map(citem => {
          return citem.value
        })[0] : "",
        order_by: this.data.typeList.filter(item => { return item.active }).length ? this.data.typeList.filter(item => { return item.active }).map(item => {
          return item.sort
        })[0] : ""
      },
      success: (respone) => {
        if (respone.data.result !== 1) {
          wx.showToast({
            title: respone.data.msg,
          });
          return false;
        }
        let res = respone.data.data;
        let finished = false;
        let goodsList = [];
        let total = 1;
        goodsList = this.data.goodsList.concat(res.data);
        if (res.data.length === 0 || res.data.length < res.per_page || res.current_page === res.last_page) {
          finished = true;
        }
        total = res.total;
        this.setData({
          finished,
          goodsList,
          page: ++this.data.page,
          total,
        });
      }
    });
  },
})